/*!
 * @file MessageActuator.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/MessageActuator>
#include <ooGame/MessageSensor>
#include <ooGame/Game>

#include <osgODE/Notify>
#include <osgODE/ODEObject>
#include <osgODE/World>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
MessageActuator::MessageActuator(void):
    m_type      ( TEXT ),
    m_dirty     ( true )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
MessageActuator::MessageActuator(const MessageActuator& other, const osg::CopyOp& copyop):
    Actuator                    ( other, copyop ),
    m_target                    ( other.m_target ),
    m_type                      ( other.m_type ),
    m_subject                   ( other.m_subject ),
    m_dirty                     ( other.m_dirty ),
    m_target_by_name_internal   ( other.m_target_by_name_internal )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
MessageActuator::~MessageActuator(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
MessageActuator::actuate( osgODE::ODEObject* object, Game* game )
{
    (void) game ;


    PS_ASSERT1( object ) ;
    PS_ASSERT1( object->getWorld() ) ;



    if( m_dirty ) {

        if( m_target_by_name_internal != "" ) {

            PS_DBG("ooGame::MessageActuator::actuate(%p, object=%p, game=%p): looking for \"%s\"", this, object, game, m_target_by_name_internal.c_str()) ;

            m_target = object->getWorld()->getObjectByName( m_target_by_name_internal, true ) ;

            PS_ASSERT1( m_target.valid() ) ;

            m_target_by_name_internal = "" ;

        }

        m_dirty = false ;
    }



    switch( m_type )
    {
        case TEXT:      text() ;            break ;
        case PROPERTY:  property() ;        break ;

        default:        PS_BREAKPOINT() ;   break ;
    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
MessageActuator::text(void)
{
    if( m_target.valid() ) {

        osgODE::ODECallback*    cbk = m_target->getUpdateCallback() ;

        while( cbk ) {

            Game*   game = cbk->asGame() ;

            cbk = cbk->getNestedCallback() ;

            if( ! game ) {
                continue ;
            }


            SensorList& sensors = game->getSensorList() ;



            for( unsigned int i=0; i<sensors.size(); i++ ) {

                MessageSensor*  ms = sensors[i]->asMessageSensor() ;

                if( ms ) {
                    ms->receiveSubject( m_subject ) ;
                }
            }

        }

    }
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
MessageActuator::property(void)
{
    PS_BREAKPOINT() ;
}
/* ....................................................................... */
/* ======================================================================= */
